use "$temp\cleaned_obs_ver$ver.dta", clear
drop if missing(spell)

merge 1:1 persnr spell using "$temp\BeH_large", keep(3) nogen

merge m:1 beh_beruf_num using "$temp/high_skill_cleaned_obs_all", keepusing(high_skill) nogen keep(1 3)
rename high_skill high_skill_current

*Create firm-skill ID variable for sender and receiver
rename betnr betnr_orig
gen double betnr = (betnr_orig * 10 + high_skill_current)
replace betnr = betnr + 1
replace betnr=1 if betnr_orig==1
format betnr %12.0g
drop if missing(betnr)

merge m:1 betnr using "$temp/estab_matrix_ids_skill_ver$ver.dta", keep(3) nogen

keep if in_strconn == 1

dis as text "syear = $syear ; eyear = $eyear"
drop if year < $syear 
drop if year > $eyear

gen ln_imp_w = log(tentgelt)
gen ln_imp_earn = ln_imp_w + log(spell_length)

tab beh_ausbildung
gen edu = beh_ausbildung
replace edu = 0 if beh_ausbildung == -7
replace edu = 5 if edu == 6
tab edu

compress
keep ln_imp_w age edu year persnr betnr ao_bula

reghdfe ln_imp_w, absorb(i.age#i.edu year persnr betFE=betnr)

keep betnr persnr year *FE*
compress
	
collapse (mean) betFE, by(betnr) fast
rename betFE firm_fe
save "$temp\AKM_wages_skill_ver$ver", replace

clear
